package com.ncepu_ideal.maven_testng.utils;

import org.yaml.snakeyaml.Yaml;

import java.io.InputStream;
import java.util.List;
import java.util.Map;

/**
 * @description: YamlReader class for reading test data from a YAML file
 * and returning it as a list of maps of key-value pairs
 * @author: chuck
 * @time: 2023/3/29 19:45
 */
public class YamlReader {

    private final String filePath;

    public YamlReader(String filePath) {
        this.filePath = filePath;
    }

    public List<Map<String, Object>> getData(String section) {
        // Load the YAML file as an input stream
        InputStream inputStream = getClass().getClassLoader().getResourceAsStream(filePath);

        // Parse the YAML file into a list of maps of key-value pairs
        Yaml yaml = new Yaml();
        Map<String, List<Map<String, Object>>> data = yaml.load(inputStream);

        // Return the section of the YAML file as a list of maps of key-value pairs
        return data.get(section);
    }
}
